package com.cheng.example.book.advise151.chap5;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 减少HashMap中元素的数量
 * 这个例子想表达的意思，Hash存储元素占用的内存更大
 * 因为负载因子还有它的扩容策略，相对于ArrayList来说
 */
public class Client78 {
    public static void main(String[] args) {

        Map<String, String> map = new HashMap<String, String>();
        List<String> list = new ArrayList<String>();
        final Runtime rt = Runtime.getRuntime();
        // JVM中止前记录信息
        rt.addShutdownHook(new Thread() {
            @Override
            public void run() {
                StringBuffer sb = new StringBuffer();

                long heapMaxSize = rt.maxMemory() >> 20;
                sb.append(" 最大可用内存：" + heapMaxSize + " M\n");

                long total = rt.totalMemory() >> 20;
                sb.append(" 堆内存大小：" + total + "M\n");

                long free = rt.freeMemory() >> 20;
                sb.append(" 空闲内存：" + free + "M");

                System.out.println(sb);
            }
        });
        for (int i = 0; i < 1200*10000; i++) {
            map.put("key" + i, "value" + i);
//            list.add("list"+i);
        }
    }
}
